package executor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import calculator.EsportaImporta;
import calculator.GeneraReport;
import dataview.CriterioRicerca;
import exceptions.DBException;
import boundary.Home;
import boundary.Splash;
import store.BaseStore;
import store.Terminologia;

public class Main {
	
	private Home home;
	private GestireCibi gc;
	private GestireRicette gr;
	private GestireMenu gm;
	private Splash splash;
	private GestireImportExport gie;
	private Timer timer = null;
	
	public Main() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		splash = new Splash();
		inizia();
		splash.dispose();
	}
	
	public void inizia() {
		new File(EsportaImporta.EXPORT_DIR).mkdirs();
		new File(GeneraReport.REPORT_DIR).mkdirs();
		splash.updateBar(33, "33%");
		try {
			new FileInputStream(BaseStore.DB).close();
			new FileInputStream(BaseStore.XSD).close();
			splash.updateBar(66, "66%");
		} catch (Exception exc) {
			try {
				BaseStore.setUp();
				Terminologia.getTerminologia().initialize();
			} catch (DBException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
		}
		splash.updateBar(100, "100%");
		createTimerTask(BaseStore.getSettingsSalvataggi());
		File tmp = new File(BaseStore.DB_TMP);
		if (tmp.exists()) {
			if (tmp.compareTo(new File(BaseStore.DB)) != 0) {
				if (JOptionPane.showConfirmDialog(splash, "L'ultima sessione non � stata chiusa in modo corretto. Ripristinare la sessione persa?", "Rispristino sessione", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					try {
						BaseStore.recuperaBackup();
					} catch (DBException e) {
						e.printStackTrace();
						JOptionPane.showMessageDialog(null, "Attenzione: impossibile ripristinare lo stato: file corrotto", "Errore", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			tmp.delete();
		}
		
		home = new Home(this);
	}
	
	public void passaACibi() {
		gc = new GestireCibi(this, home);
	}
	
	public void passaARicette() {
		gr = new GestireRicette(this, home);
	}
	
	public void passaAMenu() {
		gm = new GestireMenu(this, home);
	}
	
	public void passaAImpExp() {
		gie = new GestireImportExport(this, home);
	}
	
	public void finito() {

	}
	
	/* Main che fa partire tutto */
	public static void main(String[] args) {
		new Main();
	}
	
	public void createTimerTask(int nMinuti) {
		if (nMinuti != 0) {
			if (timer == null)
				timer = new Timer();
			else timer.cancel();
			TimerTask task = new MyTask();
			timer.schedule(task, 60 * nMinuti * 1000, 60 * nMinuti * 1000);
		} else if (timer != null) {
			timer.cancel();
			new File(BaseStore.DB_TMP).delete();
		}
	}
	
	public class MyTask extends TimerTask {
		
		@Override
		public void run() {
			try {
				BaseStore.saveBackup();
			} catch (DBException e) {
				System.out.println(e);
			}
		}
		
	}
}
